﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using M3G;

namespace MyTestProject {

    public class TestShaderAppearance {
        [Fact]
        public void TestConstructor () {
            var app = new ShaderAppearance ();

            Assert.Equal (null, app.ShaderProgram);
            Assert.Equal (0, app.ShaderUniformsCount);
        }

        [Fact]
        public void TestSetShdaerProgram () {
            var app = new ShaderAppearance ();
            var vertShader = new VertexShader ("shader/dummy.vert");
            var fragShader = new FragmentShader ("shader/dummy.frag");
            var shaderProgram = new ShaderProgram (vertShader, fragShader);
            app.SetShaderProgram (shaderProgram);

            Assert.Equal (shaderProgram, app.ShaderProgram);
        }

        [Fact]
        public void TestAddShaderUniforms () {
            var app = new ShaderAppearance ();
            var uniforms = new ShaderUniforms ();
            app.AddShaderUniforms (uniforms);

            Assert.Equal (1, app.ShaderUniforms.Length);
            Assert.Equal (uniforms, app.ShaderUniforms[0]);
        }

        [Fact]
        public void TestRemoveShaderUniforms () {
            var app = new ShaderAppearance ();
            var uniforms = new ShaderUniforms ();
            app.AddShaderUniforms (uniforms);
            app.AddShaderUniforms (uniforms);
            app.RemoveShaderUniforms ();

            Assert.Equal (0, app.ShaderUniforms.Length);
        }

        [Fact]
        public void TestReferences () {
            var app = new ShaderAppearance ();
            var vertShader = new VertexShader ("shader/dummy.vert");
            var fragShader = new FragmentShader ("shader/dummy.frag");
            var shaderProgram = new ShaderProgram (vertShader, fragShader);
            app.SetShaderProgram (shaderProgram);

            var uniforms1 = new ShaderUniforms ();
            var uniforms2 = new ShaderUniforms ();
            app.AddShaderUniforms (uniforms1);
            app.AddShaderUniforms (uniforms2);
            
            var refs = app.References;

            Assert.Equal (3, refs.Length);
            Assert.Equal (shaderProgram, refs[0]);
            Assert.Equal (uniforms1, refs[1]);
            Assert.Equal (uniforms2, refs[2]);
        }

        [Fact]
        public void TestDuplicate () {
            var vertShader = new VertexShader ("shader/dummy.vert");
            var fragShader = new FragmentShader ("shader/dummy.frag");
            var shaderProgram = new ShaderProgram (vertShader, fragShader);
            var uniforms = new ShaderUniforms ();
            var app = new ShaderAppearance ();
            app.SetShaderProgram (shaderProgram);
            app.AddShaderUniforms (uniforms);
            app.UserID = 100;

            var dup = (ShaderAppearance)app.Duplicate();

            Assert.Equal (app.ShaderProgram, dup.ShaderProgram);
            Assert.Equal (app.ShaderUniforms, dup.ShaderUniforms);
            Assert.Equal (app.UserID, dup.UserID);
        }
    }
}
